/*
 * Copyright 2004 original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jbfilter.impl;

import org.jbfilter.bean.PropertyAccessor;
import org.jbfilter.bean.ReflectionPropertyAccessor;
import org.jbfilter.core.fcomps.single.SingleFilterComponent;

abstract class _SingleFilterComponentBeanSupport<E, T, C extends SingleFilterComponent<E, T>>
	extends _FilterComponentBeanSupport<E, C> {

	_SingleFilterComponentBeanSupport(C filterComponent) {
		super(filterComponent);
	}
	
	private PropertyAccessor<E, T> propertyAccessor;
	// defaults to false
	private boolean nullTestingEnabled = false;
	
	public void setNullTestingEnabled(boolean enabled) {
		this.nullTestingEnabled = enabled;
	}

	public boolean isNullTestingEnabled() {
		return nullTestingEnabled;
	}

	public T getPropertyValue(E bean) {
		// here we must use the getter because if no propertyAccessor exists, a new one is created
		// taking the id as propertyPath
		return getPropertyAccessor().getPropertyValue(bean);
	}
	public void setPropertyAccessor(PropertyAccessor<E, T> propertyAccessor) {
		this.propertyAccessor = propertyAccessor;
	}

	@Override
	public Boolean callBackFromFilterComponentBeanSupport(E bean) {
		SingleFilterComponent<E, T> fc = getFilterComponent();
		// property look-up
		T valueToTest = getPropertyValue(bean);
		T value = fc.getValue();
		// null testing
		Boolean nullTestingPassed = nullTestingPassed(valueToTest, value);
		if (nullTestingPassed != null) {
			return nullTestingPassed.booleanValue();
		}
		if (value == null) {
			return null; // ignore filter components which values are null, otherwise all the filter would get pointless in this case
		}
		if (valueToTest == null) { // value is Not null here, so valueToTest must also be != null to pass
			return false;
		}
		return callBackFromSingleFilterComponentBeanSupport(valueToTest, value); // both values should be != null here
	}
	// null === 'cannot decide, continue evaluation'
	protected Boolean nullTestingPassed(T valueToTest, T value) {
		if( isNullTestingEnabled() && value == null ) {
			if (valueToTest == null) {
				return Boolean.TRUE;
			} else {
				return Boolean.FALSE;
			}
		} else {
			return null;
		}
	}
	public String getPropertyPath() {
		// use getter here to initialize lazily if null
		if (getPropertyAccessor() instanceof ReflectionPropertyAccessor) {
			return ((ReflectionPropertyAccessor<E, T>)propertyAccessor).getPropertyPath();
		} else {
			return null;
		}
	}
	
	public void setPropertyPath(String propertyPath) {
		if (propertyAccessor instanceof ReflectionPropertyAccessor) {
			((ReflectionPropertyAccessor<E, T>)propertyAccessor).setPropertyPath(propertyPath);
		} else {
			setPropertyAccessor(new ReflectionPropertyAccessor<E, T>(propertyPath));
		}
	}
	private PropertyAccessor<E, T> getPropertyAccessor() {
		if (propertyAccessor != null) {
			return propertyAccessor;
		}
		synchronized (this) {
			if (propertyAccessor != null) {
				return propertyAccessor;
			}
			// no propertyPath --> take id as propertyPath
			setPropertyAccessor(new ReflectionPropertyAccessor<E, T>(getFilterComponent().getId()));
		}
		return propertyAccessor;
	}
	/**
	 * @param valueToTest {@link #getPropertyValue(Object)}
	 * @param value {@link SingleFilterComponent#getValue()}
	 * @return
	 */
	abstract protected boolean callBackFromSingleFilterComponentBeanSupport(T valueToTest, T value);

}
